//
//  DockView.m
//  团购
//
//  Created by 樊荣海 on 15/1/20.
//  Copyright (c) 2015年 樊荣海. All rights reserved.
//

#import "DockView.h"


@implementation DockView {
    
    
    DockItemButton *_currentButton;
}


- (instancetype)initWithFrame:(CGRect)frame {


    if (self == [super initWithFrame:frame]) {
        
        //1.自动伸缩 高度+右边间距
//        self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
        
        self.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        
        //2.bgColor
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_tabbar"]];
        
        //3.logo
        UIImageView *logo = [[UIImageView alloc] init];
        logo.image = [UIImage imageNamed:@"ic_logo"];
        logo.bounds = CGRectMake(0, 0, logo.image.size.width*0.6, logo.image.size.height*0.6);
        logo.center = CGPointMake(DockItemW*0.5, DockItemH*0.5);
        [self addSubview:logo];
        
        //4.选项卡
        //团购
        [self addDockItem:@"ic_deal" selectedIcon:@"ic_deal_hl" index:1 bottomLine:NO];
        
        //地图
        [self addDockItem:@"ic_map" selectedIcon:@"ic_map_hl" index:2 bottomLine:NO];
        
        //收藏
        [self addDockItem:@"ic_collect" selectedIcon:@"ic_collect_hl" index:3 bottomLine:NO];
        
        //我的
        [self addDockItem:@"ic_mine" selectedIcon:@"ic_mine_hl" index:4 bottomLine:YES];
        
        //5.定位
        DockLocationButton *location = [[DockLocationButton alloc] initWithFrame:CGRectMake(0, self.frame.size.height-DockItemH*2, 0, 0)];
        [self addSubview:location];
        
        //6.更多
        DockMoreButton *more = [[DockMoreButton alloc] initWithFrame:CGRectMake(0, self.frame.size.height-DockItemH, 0, 0)];
        [self addSubview:more];
    
    
    
    }
    
    
    
    
    
    return self;
}

- (void)addDockItem:(NSString *)icon selectedIcon:(NSString *)selectedIcon index:(int)index bottomLine:(BOOL)bottomLine {

    DockItemButton *item = [[DockItemButton alloc] initWithFrame:CGRectMake(0, DockItemH*index, 0, 0)];
    [item setImage:icon selectedImage:selectedIcon];
    item.tag = index-1;
    item.bottomLine = bottomLine;
    [self addSubview:item];
    
    if (index == 1) {
        [self dockButtonClick:item];
    }
    
    [item addTarget:self action:@selector(dockButtonClick:) forControlEvents:UIControlEventTouchDown];

}


- (void)dockButtonClick:(DockItemButton *)sender {
    
    _currentButton.enabled = YES;
    sender.enabled = NO;
    if ([_delegate respondsToSelector:@selector(dockView:selectedFrom:to:)]) {
        [_delegate dockView:self selectedFrom:_currentButton.tag to:sender.tag];
    }
    
    
    _currentButton = sender;

   
}


//宽度写死
- (void)setFrame:(CGRect)frame {

    frame.size.width = DockItemW;
    [super setFrame:frame];
}

- (void)setBounds:(CGRect)bounds {

    bounds.size.width = DockItemW;
    [super setBounds:bounds];
}

@end
